//
//  VisitorView.swift
//  sinaWeiBo
//
//  Created by smartlei on 15/10/14.
//  Copyright © 2015年 smartlei. All rights reserved.
//

import UIKit
import SnapKit



class VisitorView: UIView {
        
    func setInfo(imgName: String?,msg: String) {
        
        msgLabel.text = msg
        
        guard let name = imgName else {
            //不需要设置图片则返回
            //说明为主视图开启动画
            animationStart()
            
            return
        }
        
        imgView.image = UIImage(named: name)
        //隐藏蒙板
        maskIconView.hidden = true
        //隐藏
        imgHomeView.hidden = true
    }
    
    // MARK: - 构造函数
    // initWithFrame 是 UIView 的指定构造函数
    // 使用纯代码开发使用的
    override init(frame: CGRect) {
        super.init(frame: frame)
        //设置界面
        setUI()
    }
    //MARK - 构造函数
    //initWithCoder
    //检测是否使用sb开发
    required init?(coder aDecoder: NSCoder) {
        //导致如果使用 SB 开发，调用这个视图，会直接崩溃
        //fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        //设置界面
        setUI()
    }
    
    //MARK - 懒加载中心图片控件
    private lazy var imgView: UIImageView = UIImageView(name: "visitordiscover_feed_image_smallicon")
    //使用便利构造函数替换
    //UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //MARK - 懒加载蒙板
    private lazy var maskIconView:UIImageView = UIImageView(name: "visitordiscover_feed_mask_smallicon")
    //UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //MARK - 懒加载中心房子图片
    private lazy var imgHomeView: UIImageView = UIImageView(name: "visitordiscover_feed_image_house")
    //UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //MARK - 懒加载指示信息
    private lazy var msgLabel: UILabel = UILabel(labText: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜", labTextColor: UIColor.darkGrayColor(), fontSize: 14)
    //MARK - 懒加载创建注册按钮
    //使用分类的缺省值
    //不要使用private 则父控制器能够访问到该按钮，不然访问不到，无法使用addtarget方法
     lazy var registerButton: UIButton = UIButton(title: "注册", backImgName: "common_button_white_disable")

    //MARK - 懒加载创建登录按钮
    //不要使用private 则父控制器能够访问到该按钮，不然访问不到，无法使用addtarget方法
     lazy var loginButton: UIButton = UIButton(title: "登录", titleColor: UIColor.darkGrayColor(), backImgName: "common_button_white_disable")
    
}
// MARK: - 添加动画
extension VisitorView {
    private func animationStart() {
        
        let baseAnimation: CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation")
        
        baseAnimation.toValue = 2 * M_PI
        baseAnimation.repeatCount = MAXFLOAT
        baseAnimation.duration = 10
        //动画结束禁止移除
        baseAnimation.removedOnCompletion = false
        
        imgView.layer.addAnimation(baseAnimation, forKey: nil)
    }
}

// MARK: - 设置UI
extension VisitorView {
    
    private func setUI() {
        
        //1 添加图片控件
        addSubview(imgView)
        //5 添加遮罩
        addSubview(maskIconView)
        //2 添加轮盘中心房子按钮
        addSubview(imgHomeView)
        //3 添加信息Label
        addSubview(msgLabel)
        //4 添加注册按钮
        addSubview(registerButton)
        //5 添加登录按钮
        addSubview(loginButton)
        
        
        //2 不允许子控制器自动布局
//        for subView in subviews {
//            subView.translatesAutoresizingMaskIntoConstraints = false
//        }
        //3 添加约束
        //3.1 添加中心转盘约束
//        addConstraint(NSLayoutConstraint(item: imgView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: imgView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
        imgView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-60)
        }
        
        //3.2 添加中心房子按钮
//        addConstraint(NSLayoutConstraint(item: imgHomeView, attribute: .CenterX, relatedBy: .Equal, toItem: imgView, attribute: .CenterX, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: imgHomeView, attribute: .CenterY, relatedBy: .Equal, toItem: imgView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        imgHomeView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(imgView.snp_center)
        }
        //3.3 添加信息lable的约束
//        addConstraint(NSLayoutConstraint(item: msgLabel, attribute: .Top, relatedBy: .Equal, toItem: imgView, attribute: .Bottom, multiplier: 1.0, constant: 16))
//        addConstraint(NSLayoutConstraint(item: msgLabel, attribute: .CenterX, relatedBy: .Equal, toItem: imgView, attribute: .CenterX, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: msgLabel, attribute: .Width , relatedBy: .Equal, toItem: nil
//            , attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
//        addConstraint(NSLayoutConstraint(item: msgLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        msgLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(imgView.snp_centerX)
            make.top.equalTo(imgView.snp_bottom).offset(16)
            make.width.equalTo(224)
            make.height.equalTo(36)
        }
        //        //3.4 添加注册按钮约束
        let btnH: CGFloat = 36
        let btnW: CGFloat = 100
        
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Left, relatedBy: .Equal, toItem: msgLabel, attribute: .Left, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: msgLabel, attribute: .Bottom, multiplier: 1.0, constant: 15))
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: btnW))
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: btnH))
        registerButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(msgLabel.snp_left)
            make.top.equalTo(msgLabel.snp_bottom).offset(15)
            make.width.equalTo(btnW)
            make.height.equalTo(btnH)
        }
        //        //3.5 添加登录按钮约束
        
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Right, relatedBy: .Equal, toItem: msgLabel, attribute: .Right, multiplier: 1.0, constant: 0 ))
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: msgLabel, attribute: .Bottom, multiplier: 1.0, constant: 15))
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: btnW))
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: btnH))
        loginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(msgLabel.snp_right)
            make.top.equalTo(msgLabel.snp_bottom).offset(15)
            make.width.equalTo(btnW)
            make.height.equalTo(btnH)
        }
        //3.6 设置遮罩
        /**
        VFL : 可视化格式语言
        
        H 水平方向
        V 垂直方向
        | 边界
        [] 包装控件
        views: 是一个字典 [名字: 控件名] - VFL 字符串中表示控件的字符串
        metrics: 是一个字典 [名字: NSNumber] - VFL 字符串中表示某一个数值
        */
        //设置左边和右边约束
        
//        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[maskView]-0-|", options: [], metrics: nil, views: ["maskView": maskIconView]))
        maskIconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.right.equalTo(self.snp_right)
            make.left.equalTo(self.snp_left)
            make.bottom.equalTo(registerButton.snp_bottom)
        }
        //设置上边和下边约束
        //V:|-0-[maskView]-(btnH)-[registbtn]中btnH不是上边的按钮高，为metrics的字典key值
//        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[maskView]-(btnH)-[registbtn]", options: [], metrics: ["btnH": -36], views: ["maskView": maskIconView,"registbtn":registerButton]))
        
        //设置背景颜色
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1.0)    }
}
